package com.hc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.common.bean.PageBean;
import com.hc.common.enums.ResultEnum;
import com.hc.constant.GlobalConstant;
import com.hc.domain.ProductSpecs;
import com.hc.ex.GlobalException;
import com.hc.mapper.ProductSpecsMapper;
import com.hc.query.ProductSpecsCondition;
import com.hc.res.ProductSpecsItem;
import com.hc.service.ProductSpecsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/7 15:10
 */
@Service
public class ProductSpecsServiceImpl extends ServiceImpl<ProductSpecsMapper, ProductSpecs> implements ProductSpecsService {

    @Resource
    private ProductSpecsMapper productSpecsMapper;

    @Override
    public int updateBatch(List<ProductSpecs> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<ProductSpecs> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public PageBean<ProductSpecsItem> queryByCondition(ProductSpecsCondition condition) {
        if (condition == null) {
            throw new GlobalException(ResultEnum.PARAM_ERROR);
        }
        // 获取分页的页码和大小
        final Long current = condition.getCurrent() == null ? 1 : condition.getCurrent();
        final Integer pageSize = condition.getPageSize() == null ? GlobalConstant.PageSize.DEFALUT_PAGE_SIZE : condition.getPageSize();
        IPage<ProductSpecsItem> page = new Page<>(current, pageSize);
        productSpecsMapper.selectPage(page,condition);
        PageBean<ProductSpecsItem> productSpecsItemPageBean = PageBean.init(page);
        return productSpecsItemPageBean;
    }
}